perf(desktop): 200-entry LRU cache for highlight.js output#3117
Closed
HUQIANTAO wants to merge 1 commit into
Closed
perf(desktop): 200-entry LRU cache for highlight.js output#3117HUQIANTAO wants to merge 1 commit into
HUQIANTAO wants to merge 1 commit into
Conversation
highlight.highlight() runs a real lexer walk over the source. The same code block re-renders many times in normal use — a streaming update on the *next* block still re-runs the parent transcript, the React StrictMode double-invoke in dev re-renders every block, and the code-block toolbar's hover-reveal re-renders the block's wrapper. Add a 200-entry LRU keyed on a djb2 hash of (lang + code). The cache stores the original source alongside the rendered HTML and verifies the source matches before serving — a hash collision (vanishingly unlikely with djb2 on multi-KB code) returns a cache miss instead of the wrong highlight. Map iteration is insertion-order, so the oldest entry is at the head; eviction is a single delete(). Size 200 covers the visible viewport (~30 blocks) plus an overshoot for the transcript's re-render window. Memory is bounded: each entry is the (code, html) pair, so worst case is ~200 * average-block-size bytes; in practice 50KB total for a busy transcript. Public API is unchanged. tsc --noEmit passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
highlight.highlight()runs a real lexer walk over the source. The same code block re-renders many times in normal use — a streaming update on the next block still re-runs the parent transcript, the React StrictMode double-invoke in dev re-renders every block, and the code-block toolbar's hover-reveal re-renders the block's wrapper.Add a 200-entry LRU keyed on a djb2 hash of (lang + code). The cache stores the original source alongside the rendered HTML and verifies the source matches before serving — a hash collision returns a cache miss instead of the wrong highlight. Map iteration is insertion-order, so the oldest entry is at the head; eviction is a single
delete(). Public API is unchanged.